package com.barunsw.bwf.bims.device;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.barunsw.bwf.bims.vo.DeviceVo;
import com.barunsw.bwf.common.vo.ResultVo;
import com.barunsw.bwf.constants.Result;

@Controller
@RequestMapping("/device")
public class DeviceController {
	private static Logger LOGGER = LoggerFactory.getLogger(DeviceController.class);
	
	@Autowired
	DeviceService deviceService;
	
	@RequestMapping(value="", method=RequestMethod.GET)
	public @ResponseBody List<DeviceVo> getDeviceList() throws Exception {
		List<DeviceVo> deviceList = deviceService.getDeviceList();
		return deviceList;
	}
	
	@RequestMapping(value="/{deviceId}", method=RequestMethod.GET)
	public @ResponseBody DeviceVo getDeviceById(@RequestBody @PathVariable long deviceId) throws Exception {
		DeviceVo device = deviceService.getDeviceById(deviceId);
		return device;
	}
	
	@RequestMapping(value="", method=RequestMethod.POST)
	public @ResponseBody ResultVo createDevice(@RequestBody DeviceVo deviceVo) throws Exception {
		LOGGER.debug("createDevice");
		
		ResultVo resultVo = new ResultVo(Result.OK, "");
		
		int result = deviceService.createDevice(deviceVo);
		if ( result <= 0 ) {
			resultVo.setResult(Result.NOK);
			resultVo.setReason("Device 추가시 오류가 발생하였습니다.");
		}
		
		return resultVo;
	}
	
	@RequestMapping(value="", method=RequestMethod.PUT)
	public @ResponseBody ResultVo updateDevice(@RequestBody DeviceVo deviceVo) throws Exception {
		ResultVo resultVo = new ResultVo(Result.OK, "");
		
		int result = deviceService.updateDevice(deviceVo);
		if ( result <= 0 ) {
			resultVo.setResult(Result.NOK);
			resultVo.setReason("Device 수정시 오류가 발생하였습니다.");
		}
		
		return resultVo;
	}
	
	@RequestMapping(value="/{deviceId}", method=RequestMethod.DELETE)
	public @ResponseBody ResultVo deleteDevice(@RequestBody @PathVariable long deviceId) throws Exception {
		ResultVo resultVo = new ResultVo(Result.OK, "");
		
		int result = deviceService.deleteDevice(deviceId);
		if ( result <= 0 ) {
			resultVo.setResult(Result.NOK);
			resultVo.setReason("Device 삭제시 오류가 발생하였습니다.");
		}
		
		return resultVo;
	}
	
	@RequestMapping(value="/addDevice")
	public String openAddDeviceWindow() {
		return "device/addDevice";
	}
	
	@RequestMapping(value="/updateDevice")
	public String openUpdateDeviceWindow() {
		return "device/updateDevice";
	}
	
	/*
	@RequestMapping(value="/updateDevice/{deviceId}")
	public ModelAndView openUpdateDevice(@RequestBody @PathVariable long deviceId) {
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("device/updateDevice");
		modelAndView.addObject("deviceId", deviceId);
		
		return modelAndView;
	}
	*/
}
